package co.yixiang.hw.rest;

import co.yixiang.annotation.AnonymousAccess;
import co.yixiang.aop.log.Log;
import co.yixiang.hw.domain.YxStoreCouponIssue;
import co.yixiang.hw.service.YxStoreCouponIssueService;
import co.yixiang.hw.service.YxStoreProductService;
import co.yixiang.hw.service.dto.YxStoreProductDto;
import co.yixiang.hw.service.dto.YxStoreProductQueryCriteria;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.data.domain.Pageable;


/**
* @author Walert
* @date 2020-02-13
*/
@Api(tags = "商品接口API管理")
@RestController
@RequestMapping("/api/yxStoreProduct")
public class YxStoreProductController extends BaseController {

    private final YxStoreProductService yxStoreProductService;
    private final YxStoreCouponIssueService couponIssueService;

    public YxStoreProductController(YxStoreProductService yxStoreProductService,
                                    YxStoreCouponIssueService couponIssueService) {
        this.yxStoreProductService = yxStoreProductService;
        this.couponIssueService = couponIssueService;
    }

    @GetMapping
    @Log("查询商品接口API")
    @ApiOperation("查询商品接口API")
    @AnonymousAccess
    public ResponseEntity<Object> getYxStoreProducts(YxStoreProductQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(yxStoreProductService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("商品详情")
    @ApiOperation(value = "商品详情",response = YxStoreProductDto.class)
    @GetMapping(value = "/details")
    @AnonymousAccess
    public ResponseEntity<Object> getDetails(YxStoreProductQueryCriteria criteria){
        YxStoreProductDto detail = yxStoreProductService.findById(criteria.getId());
        JSONObject result = (JSONObject)JSONObject.toJSON(detail);
        result.put("coupons",couponIssueService.getIssueCouponList(getUserId().intValue(),criteria.getId()));
        return new ResponseEntity<>(result,HttpStatus.OK);
    }
}